package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.bean.BeanStory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/18
 */
public class StoryLayout extends FrameLayout {
    private static final String TYPE1="0";
    private static final String TYPE2="1";
    private static final String TYPE3="2";
    private LayoutInflater mLayoutInflater;
    private List<StoryView> listView;

    public StoryLayout(@NonNull Context context) {
        this(context,null,0);
    }

    public StoryLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StoryLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mLayoutInflater = LayoutInflater.from(context);
    }

    private String type;
    private List<BeanStory> stringList;
    public void initType(String type,List<BeanStory> stringList){
        removeAllViews();
        this.type=type;
        this.stringList=stringList;
        switch (type){
            case TYPE1:
                listView = getLayout(R.layout.view_story_1);
                initData(stringList,listView);
                break;
            case TYPE2:
                listView = getLayout(R.layout.view_story_2);
                initData(stringList,listView);
                break;
            case TYPE3:
                listView = getLayout(R.layout.view_story_4);
                initData(stringList,listView);
                break;

        }
    }


    private List<StoryView> getLayout(int layout){
        View view=mLayoutInflater.inflate(layout,null,false);
        addView(view);
        List<StoryView> listView=new ArrayList<>();
        getChildAtView(view,listView);
       return listView;
    }




    private void getChildAtView(View view,List<StoryView> list){
        int count=((ViewGroup)view).getChildCount();
        for (int i=0;i<count;i++){
            View views=  ((ViewGroup)view).getChildAt(i);
            if(views instanceof StoryView){
                list.add((StoryView) views);
            }else if(views instanceof ViewGroup){
                 getChildAtView(views,list);
            }
        }

    }


    private void initData(List<BeanStory> stringList,  List<StoryView> listView){
        if(stringList!=null&&listView!=null&&stringList.size()>0&&listView.size()>0){
            int size= Math.min(stringList.size(), listView.size());
            for (int i=0;i<size;i++){
                final BeanStory beanStory   =stringList.get(i);
                listView.get(i).setTitle(beanStory.getStoryTitle());
                listView.get(i).setContent(beanStory.getStoryIntro());
                listView.get(i).setPathImage(beanStory.getStoryCoverImg());
                listView.get(i).setLabel(beanStory.getSkipType());
                listView.get(i).setTime(beanStory.getCreateTime());
                listView.get(i).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(onClickDataListener!=null){
                            onClickDataListener.setOnClickDataListener(beanStory);
                        }
                    }
                });
            }
        }
    }

    private OnClickDataListener onClickDataListener;

    public interface OnClickDataListener{
        void setOnClickDataListener(BeanStory beanStory);
    }


    public void setOnClickDataListener(OnClickDataListener onClickDataListener) {
        this.onClickDataListener = onClickDataListener;
    }
}
